﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Mock1G3.BaseEntities;
using Mock1G3.DataAccess;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.SqlClient;

namespace Mock1G3.BussinessLogic
{
    public class TransferCashLogic
    {
        /// <summary>
        /// Check string is number
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public bool IsNumber(string str)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(str);
        }

        /// <summary>
        /// Check balance greater amount customer entered
        /// </summary>
        /// <param name="account"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public bool CheckBalanceAmount(Account account, string amount)
        {
            decimal transAmount = decimal.Parse(amount);
            try
            {
                decimal balance = TransferCashAccess.GetBalanceAccount(account.AccountNo);
                if (balance > transAmount)
                {
                    return true;
                }
            }
            catch { }
            return false;
        }

        /// <summary>
        /// Limit max amount transfer is 30.000000
        /// </summary>
        /// <param name="amount"></param>
        /// <returns></returns>
        public bool CheckTransferLimit(string amount)
        {
            if (IsNumber(amount))
            {
                try
                {
                    DataTable dt = TransferCashAccess.GetTransferLimit();
                    DataRow dr = dt.Rows[0];
                    decimal minTransfer = decimal.Parse(dr["MinWithDraw"].ToString());
                    decimal maxTransfer = decimal.Parse(dr["MaxWithDraw"].ToString());
                    decimal transAmount = decimal.Parse(amount);
                    if ((transAmount >= minTransfer) && (transAmount <= maxTransfer))
                        return true;
                }
                catch
                {
                    throw;
                }
            }
            return false;
        }

        /// <summary>
        /// Transfer Cash
        /// </summary>
        /// <param name="account"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public bool TransferCash(Account account, decimal amount)
        {
            if (TransferCashAccess.TransferCash(account.AccountNo, account.AccountNoRev, amount))
                return true;
            return false;
        }

        /// <summary>
        /// Check receive account and send account are different
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public static bool CheckReceiveSendAccount(Account account)
        {
            if (account.AccountNo != account.AccountNoRev)
                return true;
            return false;
        }

        /// <summary>
        /// Write Log
        /// </summary>
        /// <param name="log"></param>
        /// <returns></returns>
        public bool WriteLog(Log log)
        {
            if (TransferCashAccess.WriteLog(log) == true)
                return true;
            return false;
        }
    }
}